﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SmallERP.COM
{
    public class DxfExtend
    {
        const string flagEXTMin = @"$EXTMIN";
        const string flagEXTMax = @"$EXTMAX";
        const string flagRowEnd = "  9";
        List<string> listExtMin = new List<string>();
        List<string> listExtMax = new List<string>();
        private bool bEXTMin = false;
        public static List<DxfObjecctInfo> ListDXFObj = new List<DxfObjecctInfo>();
        public static void GetDxfInfo(byte[] inByteArr)
        {
            using (MemoryStream stream = new MemoryStream(inByteArr))
            {
                using (StreamReader streamReader = new StreamReader(stream))
                {

                    string readerText = "";


                    bool flagGet = false;
                    bool flagEnd = false;
                    while ((readerText = streamReader.ReadLine()) != null)
                    {
                        flagEnd = false;
                        switch (readerText)
                        {
                            case flagEXTMin:
                                ListDXFObj.ForEach(each => { each.Active = false; });
                                ListDXFObj.Add(new DxfObjecctInfo()
                                {
                                    KeyString = flagEXTMin,
                                    Active = true,
                                    ContentList = new List<string>()
                                });
                                break;
                            case flagEXTMax:
                                ListDXFObj.ForEach(each => { each.Active = false; });
                                ListDXFObj.Add(new DxfObjecctInfo()
                                {
                                    KeyString = flagEXTMax,
                                    Active = true,
                                    ContentList = new List<string>()
                                });
                                break;
                            case flagRowEnd:
                                flagEnd = true;
                                break;
                            default:
                                break;

                        }
                        DxfObjecctInfo currInfo = ListDXFObj.FirstOrDefault(p => p.Active);
                        if (currInfo != null)
                        {
                            if (currInfo.ContentList == null)
                            {
                                currInfo.ContentList = new List<string>();
                            }
                            currInfo.ContentList.Add(readerText);

                        }
                        if (flagEnd)
                        {
                            ListDXFObj.ForEach(each => { each.Active = false; });
                        }

                    }
                }
            }
            //streamReader
        }

        public static DXFPointer AlyseEXTVaue(List<string> inList)
        {
            DXFPointer dxfPointer = new DXFPointer();
            if (inList.Count == 8)
            {
                int iRow = 0;
                foreach (string lineValue in inList)
                {

                    if (lineValue == " 10")
                    {
                        dxfPointer.X = Convert.ToDouble(inList[iRow + 1]);
                    }
                    else if (lineValue == " 20")
                    {
                        dxfPointer.Y = Convert.ToDouble(inList[iRow + 1]);
                    }
                    if (lineValue == " 30")
                    {
                        dxfPointer.Z = Convert.ToDouble(inList[iRow + 1]);
                    }
                    iRow++;
                }
            }
            return dxfPointer;
        }

        public static byte[] GetFileContent(string Path)
        {
            byte[] data;
            using (FileStream fs = File.OpenRead(Path))
            {
                data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
            }
            return data;
        }
    }

    public class DxfObjecctInfo
    {
        public string KeyString { get; set; }
        public bool Active { get; set; }
        public List<string> ContentList { get; set; }


    }



    public class DXFInfo
    {
        public DXFPointer MinPointer { get; set; }     //左下坐标
        public DXFPointer MaxPointer { get; set; }    //右上坐标

    }

    public class DXFPointer
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }
    }
}
